package org.zcl.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.zcl.dao.TbUserInfoDao;
import org.zcl.entity.po.TbUserInfo;
import org.zcl.entity.request.TbUserRequest;
import org.zcl.entity.response.UserVO;
import org.zcl.entity.model.PageResponse;
import org.zcl.exception.BusinessException;
import org.zcl.service.ActivityService;
import org.zcl.service.TbUserInfoService;

import java.util.Objects;

/**
 * 用户表(TbUserInfo)表服务实现类
 *
 * @author Chenglin Zhu
 * @since 2022-01-28 10:13:39
 */
@Service("tbUserInfoService")
@Slf4j
public class TbUserInfoServiceImpl extends ServiceImpl<TbUserInfoDao, TbUserInfo> implements TbUserInfoService {

    @Autowired
    private TbUserInfoDao tbUserInfoDao;

    @Autowired
    private ActivityService activityService;

    @Autowired
    private TbUserInfoService tbUserInfoService;

    @Override
    public void update(UserVO userVO) {
        TbUserInfo userInfo = tbUserInfoService.selectById(userVO.getId());
        userInfo.setId(userVO.getId());
        userInfo.setName(userVO.getName());

        updateName(userInfo);
        activityService.updateName(userInfo);
    }

    @Override
    public void updateUser(UserVO userVO) {
        LambdaUpdateWrapper<TbUserInfo> wrapper = new LambdaUpdateWrapper<TbUserInfo>()
                .eq(TbUserInfo::getId, userVO.getId())
                .set(TbUserInfo::getName, userVO.getName())
                .set(TbUserInfo::getAge, userVO.getAge());
        update(wrapper);
    }

    @Override
    public TbUserInfo selectById(Long id) {
        return tbUserInfoDao.selectById(id);
    }

    @Override
    public UserVO selectUserById(Long id) {
        TbUserInfo tbUserInfo = tbUserInfoDao.selectById(id);
        if (Objects.isNull(tbUserInfo)) {
            throw new BusinessException("查询结果为空！");
        }
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(tbUserInfo, userVO);
        return userVO;
    }

    @Override
    public PageResponse<TbUserInfo> page(TbUserRequest request) {
        Page<TbUserInfo> medalPOPage = new Page<>(request.getPageIndex(), request.getPageSize());
        PageResponse<TbUserInfo> pageResponse = new PageResponse<>();
        Page<TbUserInfo> page = this.page(medalPOPage);

        pageResponse.setTotalCount((int) medalPOPage.getTotal());
        pageResponse.setPageSize(request.getPageSize());
        pageResponse.setPageIndex(request.getPageIndex());
        pageResponse.setData(page.getRecords());
        return pageResponse;
    }

    @Override
    public void deleteUserById(Long id) {
        tbUserInfoDao.deleteById(id);
    }

    @Override
    public void deleteUser(Long id) {
        tbUserInfoDao.delete(new LambdaQueryWrapper<TbUserInfo>().eq(TbUserInfo::getId, id));
    }

    @Override
    public void deleteWithFill(Long id) {
        TbUserInfo tbUserInfo = new TbUserInfo();
        LambdaUpdateWrapper<TbUserInfo> wrapper = new LambdaUpdateWrapper<TbUserInfo>()
                .eq(TbUserInfo::getId, id);
        tbUserInfoDao.deleteWithFill(tbUserInfo, wrapper);
    }

    @Override
    public void updateUserWithFill(UserVO userVO) {
        TbUserInfo tbUserInfo = new TbUserInfo();
        tbUserInfo.setId(userVO.getId());
        tbUserInfo.setName(userVO.getName());
        tbUserInfo.setAge(userVO.getAge());
        tbUserInfoDao.updateById(tbUserInfo);
    }

    @Override
    public void updateByIdUserWithFill(UserVO userVO) {
        TbUserInfo tbUserInfo = new TbUserInfo();
        LambdaUpdateWrapper<TbUserInfo> wrapper = new LambdaUpdateWrapper<TbUserInfo>()
                .eq(TbUserInfo::getId, userVO.getId())
                .set(TbUserInfo::getName, userVO.getName())
                .set(TbUserInfo::getAge, userVO.getAge());
        tbUserInfoDao.update(tbUserInfo, wrapper);
    }

    @Override
    public void deleteByIdWithFill(Long id) {
        TbUserInfo tbUserInfo = new TbUserInfo();
        tbUserInfo.setId(id);
        tbUserInfoDao.deleteByIdWithFill(tbUserInfo);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void updateName(TbUserInfo userInfo) {
        try {
            Thread.sleep(20000);
            log.info("事务1执行更新，userInfo:{}", JSON.toJSONString(userInfo));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        tbUserInfoDao.updateById(userInfo);
    }
}

